<template>
  <div>
    <!-- 下拉框 -->
    <el-row type="flex" justify="end">
      <el-select v-model="year" size="small" @change="deteChangeFn">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="month" size="small" @change="deteChangeFn">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <!-- 日历 -->
    <el-calendar v-if="showcalendar" v-model="curDate">
      <template v-slot:dateCell="{date,data}" class="content">
        <div class="date-content">
          <span class="text">
            {{ data.day| formatertwo }}
          </span>
          <span v-if="isweek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>

  </div>
</template>

<script>
const curDate = new Date()
const year = curDate.getFullYear()
const month = curDate.getMonth() + 1
const yearList = []
for (let i = 0; i < 11; i++) {
  yearList.push(year - 5 + i)
}
export default {
  filters: {
    formatertwo(val) {
      return Number(val.split('-')[2])
    }
  },
  data() {
    return {
      curDate: curDate,
      year,
      month,
      yearList,
      showcalendar: true
    }
  },
  watch: {
    // 日历关联下拉框年月
    curDate(date) {
      // date是监听到的最新值
      this.year = date.getFullYear()
      this.month = date.getMonth() + 1
    },
    // 解决语言变化的对象，日历组件不能实时更新语言的问题
    '$i18n.locale'() {
      this.showcalendar = false
      this.$nextTick(() => {
        this.showcalendar = true
      })
    }
  },
  methods: {
    deteChangeFn() {
      console.log(this.year, this.month)
      this.curDate = new Date(`${this.year}-${this.month}`)
    },
    isweek(date) {
      return date.getDay() === 0 || date.getDay() === 6
    }
  }

}
</script>

<style  scoped>
 /deep/ .el-calendar-day {
  height:  auto;
 }
 /deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 /deep/ .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 /deep/ .el-calendar__header {
   display: none
 }
</style>
